<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <link rel="stylesheet" href="./bootstrap.css">
    <script src="./moment.js"></script>
    <script src="./vuev2.5.21.js"></script>
</head>

<body>
    <div id="app">
        <div class="panel panel-primary">
            <div class="panel-heading">
                <h3 class="panel-title">添加品牌</h3>
            </div>
            <div class="panel-body form-inline">
                <label>
                    id:
                    <input type="text" name="id" class="form-control" v-model="id" v-color="'pink'">
                </label>
                <label>
                    Name:
                    <input type="text" name="name" id="id" class="form-control" v-model="name" @keyup.enter="add"
                        v-color="'red'">
                </label>
                <input type="button" value="添加" class="btn btn-primay" @click="add()">
                <label>
                    搜索名称关键字:
                    <input type="text" class="form-control" v-model="keywords" autofocus v-color="'green'">
                </label>
            </div>
        </div>
        <table class="table table-bordered table-hover table-striped">
            <thead>
                <tr>
                    <th>id</th>
                    <th>Name</th>
                    <th>Ctime</th>
                    <th>Operation</th>
                </tr>
            </thead>
            <tbody>
                <tr v-for="item in search(keywords)" :key="item.id">
                    <td>{{item.id}}</td>
                    <td v-text="item.name"></td>
                    <td>{{item.ctime | msgFormat}}</td>
                    <td>
                        <a href="javajscript:''" @click.prevent="del(item.id)">删除</a>
                    </td>
                </tr>
            </tbody>
        </table>
    </div>
    <script>
        //全局自定属性
        // Vue.directive('color',{
        // 	bind:(el,binding)=>{
        // 		el.style.color=binding.value
        // 	}
        // })
        new Vue({
            el: "#app",
            data: {
                id: null,
                name: null,
                keywords: "",
                list: [{
                        id: 1,
                        name: '宝马',
                        ctime: new Date()
                    },
                    {
                        id: 2,
                        name: '奔驰',
                        ctime: new Date()
                    },
                ]
            },
            methods: {
                add() {
                    this.list.push({
                        id: this.id,
                        name: this.name,
                        ctime: new Date()
                    })
                    this.id = this.name = ""
                },
                del(id) {
                    // for(var i = 0; i < this.list.length;i++){
                    //     if(this.list[i].id === id){
                    //         this.list.splice(i , 1)
                    //         break;
                    //     }
                    // }
                    this.list.splice(this.list.findIndex(item => this.id === id), 1)
                },
                search(keywords) {
                    return this.list.filter(item => item.name.includes(keywords))
                }
            },
            filters: {
                msgFormat: (datetimesss) => {
                    return moment(datetimesss).format('YYYY-MM-DD  HH:mm:ss a');
                }
            },
            //私有自定属性
            directives: {
                'color': {
                    bind: (el, binding) => {
                        el.style.color = binding.value
                    }
                }
            }
        })
    </script>
</body>

</html>